package com.example.keli.ui.userinfo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.example.keli.R;
import com.example.keli.base.widget.PinnedHeaderListView;

import java.util.Arrays;
import java.util.List;

/** 
 * description: 选择地址
 * author: Allen
 * date: 2019/3/27 11:34 
 */
public class SelectAddressAdapter extends BaseAdapter implements SectionIndexer, PinnedHeaderListView.PinnedHeaderAdapter, AbsListView.OnScrollListener {

    private int mLocationPosition = -1;
    // 首字母集
    private List<String> mFriendsSections;
    private List<Integer> mFriendsPositions;
    private List<AddressData.ChildrenBean> cates;
    private LayoutInflater mLayoutInflater;
    private Context context;
    SelectAddressActivity.SelectCall selectCall;

    public SelectAddressAdapter(Context context, List<AddressData.ChildrenBean> cates, List<String> friendsSections, List<Integer> friendsPositions) {
        this.cates = cates;
        this.context = context;
        mFriendsSections = friendsSections;
        mFriendsPositions = friendsPositions;
    }
    public void setCallBack(SelectAddressActivity.SelectCall brandCall){
        this.selectCall = brandCall;
    }
    @Override
    public int getCount() {
        return cates == null || cates.size() == 0 ? 0 : cates.size();
    }

    @Override
    public Object getItem(int position) {
        return cates.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, final ViewGroup parent) {
        int section = getSectionForPosition(position);
        if (mLayoutInflater == null) {
            mLayoutInflater = LayoutInflater.from(parent.getContext());
        }
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.adapte_find_brand, null, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (getPositionForSection(section) == position) {
            viewHolder.mHeaderParent.setVisibility(View.VISIBLE);
            viewHolder.mHeaderText.setText(mFriendsSections.get(section));
        } else {
            viewHolder.mHeaderParent.setVisibility(View.GONE);
        }

        final AddressData.ChildrenBean catesData = cates.get(position);
        if (catesData != null) {
            viewHolder.mBrandText.setText(catesData.getLabel());
            viewHolder.mBrandLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //这里返回品牌id
                    if(null!=selectCall){
                        selectCall.selectCall(String.valueOf(catesData.getValue()),catesData.getLabel());
                    }
                }
            });
        }

        return convertView;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (view instanceof PinnedHeaderListView) {
            ((PinnedHeaderListView) view).configureHeaderView(firstVisibleItem);
        }
    }

    @Override
    public int getPinnedHeaderState(int position) {
        int realPosition = position - 1;
        if (realPosition < 0 || (mLocationPosition != -1 && mLocationPosition == realPosition)) {
            return PINNED_HEADER_GONE;
        }
        mLocationPosition = -1;
        int section = getSectionForPosition(realPosition);
        int nextSectionPosition = getPositionForSection(section + 1);
        if (nextSectionPosition != -1 && realPosition == nextSectionPosition - 1) {
            return PINNED_HEADER_PUSHED_UP;
        }
        return PINNED_HEADER_VISIBLE;
    }

    @Override
    public void configurePinnedHeader(View header, int position, int alpha) {
        int realPosition = position;
        int section = getSectionForPosition(realPosition);
        String title = (String) getSections()[section];
        ((TextView) header.findViewById(R.id.friends_list_header_text)).setText(title);
    }

    @Override
    public Object[] getSections() {
        return mFriendsSections.toArray();
    }

    @Override
    public int getPositionForSection(int section) {
        if (section < 0 || section >= mFriendsSections.size()) {
            return -1;
        }
        return mFriendsPositions.get(section);
    }

    @Override
    public int getSectionForPosition(int position) {
        if (position < 0 || position >= getCount()) {
            return -1;
        }
        int index = Arrays.binarySearch(mFriendsPositions.toArray(), position);
        return index >= 0 ? index : -index - 2;
    }

    private static class ViewHolder {
        private ImageView mBrandImage;
        private TextView mBrandText;
        private LinearLayout mHeaderParent;
        private TextView mHeaderText;
        private LinearLayout mBrandLayout;

        public ViewHolder(View view) {
            mHeaderText = (TextView) view.findViewById(R.id.friends_item_header_text);
            mHeaderParent = (LinearLayout) view.findViewById(R.id.friends_item_header_parent);
            mBrandImage = (ImageView) view.findViewById(R.id.find_brand_image);
            mBrandText = (TextView) view.findViewById(R.id.find_brand_name);
            mBrandLayout = (LinearLayout) view.findViewById(R.id.adapter_but_brand_layout);
        }
    }

//    private void setElseData(){
//        BrandBean.DataBean.BrandData brandData = new BrandBean.DataBean.BrandData();
//        brandData.setBrand_id("370");
//        brandData.setImg("");
//        brandData.setName_cn("其他");
//        cates.add(0,brandData);
//    }
}
